package com.proj;

import com.alibaba.fastjson.JSONObject;

public class Game {
    private String id;
    private User theFirstGamer;
    private User theSecondGamer;
    private Board theBoard;
    private JSONObject msg=null;
    private int state=0;//表示现在游戏里的人数
    private int round=0;
    public int addUser(User user){
        if(state==0){
            theFirstGamer=user;
            state++;
        }else if(state==1){
            theSecondGamer=user;
            state++;
            theFirstGamer.setState(true);//将user状态设置为游戏中
            theSecondGamer.setState(true);
        }else if(state==2){
            return 3;//state还是2，只是return了3
        }
        return state;
    }
    public void removeUser(String userName){
        if(theFirstGamer!=null&&userName.equals(theFirstGamer.getName())){
            theFirstGamer.setState(false);
            theFirstGamer=null;
            state--;
        }else if(theSecondGamer!=null&&userName.equals(theSecondGamer.getName())){
            theSecondGamer.setState(false);
            theSecondGamer=null;
            state--;
        }
    }
    public Boolean isInGame(String userName){
        Boolean isIn=false;
        if(userName.equals(theFirstGamer.getName()))
            isIn=true;
        if(userName.equals(theSecondGamer.getName()))
            isIn=true;
        return  isIn;
    }
    public int getPosition(String userName){
        int position=-1;
        if(userName.equals(theFirstGamer.getName())){
            position=1;
        }else if(userName.equals(theSecondGamer.getName())){
            position=2;
        }
        return position;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getId() {
        return id;
    }

    public JSONObject getMsg(String userName) {//如果俩人只set不get是没问题的，只保证set同步，get到的只有最新的且是对方的消息
        if(this.round%2==0&&userName.equals(this.theFirstGamer.getName())){//双数代表上个设置消息的是第二人，此时只有第一人可以获取消息
            JSONObject rmsg=(JSONObject)this.msg.clone();System.out.println(this.msg.clone());
            rmsg.put("theOtherUser",this.theSecondGamer.getName());
            return rmsg;
        }else if(this.round%2==1&&userName.equals(this.theSecondGamer.getName())){
            JSONObject rmsg=(JSONObject)this.msg.clone();
            rmsg.put("theOtherUser",this.theFirstGamer.getName());
            return rmsg;
        }else {
            //消息还没发，wait
            return null;
        }
    }
    public JSONObject getAMsg(){
        if(this.msg!=null)
        return (JSONObject)this.msg.clone();
        return null;
    }
    public void setAMsg(JSONObject msg){
        this.msg=msg;
    }
    public void setMsg(String userName,JSONObject msg){
        if(this.round%2==0&&userName.equals(this.theFirstGamer.getName())){//双数代表第二个人行动结束，轮到第一个人行动了
            this.round++;
            this.msg=msg;
        }else if(this.round%2==1&&userName.equals(this.theSecondGamer.getName())){
            this.round++;
            this.msg=msg;
        }
        return;
    }

    public int getRound() {
        return round;
    }

    public int getState() {
        return state;
    }
}
